package com.crm.service.system.impl;

import com.crm.mapper.system.HomePageMapper;
import com.crm.pojo.Transform;
import com.crm.service.system.HomePageService;

import com.crm.vo.HomePage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;

@Service
@Slf4j
public class HomePageServiceImpl implements HomePageService {
    @Autowired
    private HomePageMapper homePageMapper;

    @Override
    @Transactional
    public HomePage getInfo(LocalDate beginCreatetime1, LocalDate endCreateTime1) {
        LocalDateTime beginCreateTime = LocalDateTime.of(beginCreatetime1, LocalTime.of(0, 0));
        LocalDateTime endCreateTime = LocalDateTime.of(endCreateTime1, LocalTime.of(0, 0));
        LocalDate now = LocalDate.now();
        LocalDate tomorrow = now.plusDays(1);
        Integer cluesNum = homePageMapper.queryCluesNum(beginCreateTime, endCreateTime);
        Integer businessNum = homePageMapper.queryBusinessNum(beginCreateTime, endCreateTime);
        Integer contractNum = homePageMapper.queryContractNum(beginCreateTime, endCreateTime);
        Integer salasAmount = homePageMapper.querySalasAmount(beginCreateTime, endCreateTime);
        Integer todayCluesNum = homePageMapper.queryTodayCluesNum(now, tomorrow);
        Integer todayBusinessNum = homePageMapper.queryTodayBusinessNum(now, tomorrow);
        Integer todayContractNum = homePageMapper.queryTodayContractNum(now, tomorrow);
        Integer todaySalasAmount = homePageMapper.queryTodaySalasAmount(now, tomorrow);

        Integer tofollowedCluesNum = homePageMapper.queryTofollowedCluesNum(beginCreateTime, endCreateTime);
        Integer tofollowedBusinessNum = homePageMapper.queryTofollowedBusinessNum(beginCreateTime, endCreateTime);
        Integer toalllcatedCluesNum = homePageMapper.queryToalllcatedCluesNum(beginCreateTime, endCreateTime);
        Integer toalllcatedBusinessNum = homePageMapper.querytoalllcatedBusinessNum(beginCreateTime, endCreateTime);
        return new HomePage(cluesNum, businessNum, contractNum, salasAmount, todayCluesNum, todayBusinessNum, todayContractNum, todaySalasAmount, tofollowedCluesNum, tofollowedBusinessNum, toalllcatedCluesNum, toalllcatedBusinessNum);


    }

    @Override
    public Transform getTransform(String beginCreateTime, String endCreateTime) {
        // 定义日期格式，这里我们假设日期字符串的格式为"yyyy-MM-dd"
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate begindate = null;
        LocalDate enddate = null;

        try {

            begindate = LocalDate.parse(beginCreateTime, formatter);
            enddate = LocalDate.parse(endCreateTime, formatter);

        } catch (DateTimeParseException e) {
            log.info("==========================日期转化格式有误==========================");
            System.err.println("日期字符串格式错误: " + e.getMessage());


        }
        LocalDateTime beginTime = LocalDateTime.of(begindate, LocalTime.of(0, 0));
        LocalDateTime endTime = LocalDateTime.of(enddate, LocalTime.of(0, 0));

        Integer cluesNum = homePageMapper.queryCluesNum(beginTime, endTime);
        Integer effectiveCluesNum = homePageMapper.queryEffectiveCluesNum(beginTime, endTime);
        Integer businessNum = homePageMapper.queryyxBusinessNum(beginTime, endTime);
        Integer contractNum = homePageMapper.queryContractNum(beginTime, endTime);
        return new Transform(cluesNum, effectiveCluesNum, businessNum, contractNum);

    }

}
